package com.plusjun.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.plusjun.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2088102181151134";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCb5KDbiVYOyKyosc20CqASRoxDTGb48yJBOgvGUvzaHYR3I1niv/EkntB2oQZFO8GLfQvtJLk+R263t61jMmTOWPxEzbX0+OOqpJScOkm+awqyAdfyaFtWB1IoZWjzT0VJQvPt0kXkJNqnffgLlO7VfHNBVqGdpV/jTp33shtpKtgXaAbsFERJCD7aKijQpkRQHDPJ+htCbtBxTWCop7tQHt+a1bcRP26sV0vLSXEObeGnmNFzwtr1wzRIUixriybaxcaVhnlkObx0EBG13qQZVoVJuqC8++nIb7E+dJsUZUVZHY8Cok+nvk/iSrUAydrLaJzJ5TkSbwf5cOg0pQRXAgMBAAECggEAQsg3bLSxrBWleYLp1yXyGsIkFjRlrGIjm4OtRaGJdjrM42i+ede2CvUJ4rfqgsAtkn7YHBfuT1K0iHlkCqms64TNVQA24aG5jgGBNEf4DV23sDf/9XVlIz5xoKKIyip8SP0omAnLLQfor3XUS8naYpQ0n5noFCujHzIvk8u12F/5nV9cjaeGXr7hlfvEsGhFbi71J075lAZ2/FMsDw+viRSlHoRLFUarGpVhjr0wg0AK6agfUAJLcRZ2ob5ObYkTQfY8kaim5KNCgio+4la1Btt0et+VLm63zQFMN54x2Jkfpedr8IM+CdB9WB/RyUkfIBfbyJ18yWpljDdBPyefaQKBgQDXPUSDkNuRgOo/TpfIQmMIjKXmOqvh+yN9xzdtYUsOkZagBO/HUdpGGFE0Ys6cYFdMF+dXSNqXB6GdXV2s5zbVP9blA2mJ1ONnfhSHOKZie3Cd01LyRoU0GDGpXzkLP1quV2evkzSKBT+dEyf0ezdmua8EhM7p7NBzviJuKQlpAwKBgQC5akYlRa7a09BXtmqDraYNJ7KQSTqvGQbjk5ImI6W+lQFW317qFsnZkuXWl9yKpw/cycJWZRF3qinMdZegFI0QjdD6Gay80C1Q2owmVghO6loK5M0h9z6HZ+L7Lg7JPmVhPoCdC2g0agBcc7QnxycW1yX376dxj5CZznrXwse1HQKBgFssf8Y/aVLkSdXRQ+g3ft3TPj8dIUwTZsBTz/3MVc/xCd302WEUIiLJEDThmeWRxcjc40kM+lM5j1MqhpDETGzeovfVjzIsdLuVI7r32zsiUNz59EveZXRgv0Cgrng+OPpa2ff8oifI+15dC/K68wfXpfSDEWbzwoUUyF7jkTHjAoGAOpyisfOWM6yThraucxO4kx4Cb2QGvgUuTVB/+bh5E4bs1nZj/aHYiwPJzg1RdS8cb5ID8eWfIPHUpsJhfQQiF6KM9hKZrVItkQqVFSVLGQ8yCZGMWvladdlcQkSr/efQim4EoCBT/4WmuZMF1zE4ZRr78KV8jARzFHvWD1COau0CgYBLArrDtdwqwuZ0yLh9YFk6jMAyb6xtNSaQfzaNTXkpM/nFzPVa0liRyMyf+FF/8CdXGBtLdNwLLWn+KZgaIvA169Nwbw9eksyYzWWrLeh0wMViwGQNglyT8dDzVpzye2pME6+tGNqcHRg4tVsR965cJA9a4WCdeMx8dx9MDHn7Nw==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqj8IISdScyjfsdlEzw4RqbhuTCLugB6zKLyWo7HGQWS3TsOcjEpupo/zKdfu9Q0X9rSzKYSykR2wGOWn1R7Bm0Y5EI+cZKa9vqJzZ5gc87o10HJVs1uNCJoZmJqkt90+ntrWm9kssBL0EqZGO3wnZvrSn/whbrSmv+gb2Jc3d5ssdTn9uj1wghnbvxnHIcAS/3Us/GuViDU/zFzWd/be5r3Glrid0C0vZ/YsgqnULtq4TTpvsMkEBah+VjRRqvmZTZCshxwHH/lAcZ5FPz4K9xEUHpeNE4z3ltCFJHoDCP4wJ9cwxKRAAgr9mx4LTmSt8GYu9iY2jKhywRshhBBpHwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url;
    // 订单超时时间
    String timeout_express;

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout_express + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
